package com.junko.admin.configuration;

import com.junko.admin.interceptor.BackendInterceptor;
import com.junko.admin.interceptor.ErrorPageHandler;
import com.junko.admin.interceptor.SystemInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *   @description :  web配置
 *   ---------------------------------
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Value("${web.upload-path}")
    private String baseUploadPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(errorPageHandler()).addPathPatterns("/**");
        registry.addInterceptor(systemInterceptor()).addPathPatterns("/b/**");
        registry.addInterceptor(backendInterceptor()).addPathPatterns("/b/**").excludePathPatterns("/b/login/**");
    }
    @Bean
    public ErrorPageHandler errorPageHandler(){
        return new ErrorPageHandler();
    }
    @Bean
    public SystemInterceptor systemInterceptor(){
        return new SystemInterceptor();
    }
    @Bean
    public BackendInterceptor backendInterceptor(){
        return new BackendInterceptor();
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
        registry.addResourceHandler("/assets/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/assets/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ baseUploadPath);
    }
}